package com.beijiteshop.shop.ui.login.vm

import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.event.LoginEvent
import com.beijiteshop.libcommon.login.LoginInfo
import com.beijiteshop.libcommon.login.UserInfo
import com.beijiteshop.libcommon.login.UserInfoUtil
import com.beijiteshop.libcommon.network.ext.*
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.shop.model.api.request.LoginRequest
import com.beijiteshop.shop.model.api.response.UserBindingInfoBean
import com.beijiteshop.shop.ui.login.repo.UserRepo

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/12 20:15
 * @Description
 */
class UserVM(private val repo: UserRepo) : BaseViewModel() {

    val liveSmsCode = MutableLiveData<Nothing>()
    val liveLogin = MutableLiveData<String>()
    val liveWXLogin = MutableLiveData<String>()
    val liveUserInfo = MutableLiveData<UserInfo>()
    val liveBindingInfo = MutableLiveData<UserBindingInfoBean>()
    val liveBindWX = MutableLiveData<Boolean>()
    val liveBindPhone = MutableLiveData<Boolean>()

    fun getSmsCode(phone: String) = launch {
        repo.getSmsCode(phone)
            .serverData()
            .onSuccess {
                onBizOk { msg,data->
                    liveSmsCode.postValue(null)
                }

                onBizError { code, msg ->
                    liveError.postValue(msg)
                }
            }.onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun login(phone: String,password:String) = launch {
        val request = LoginRequest(phone, password)
        repo.login(request)
            .serverData()
            .onSuccess {
                onBizOk {msg,data->
                    if (data != null){
                        liveLogin.postValue(data as? String)
                        val token = data as? String
                        token?.let {
                            saveLoginInfo(phone,token)
                        }
                        EventUtil.post(LoginEvent.LOGIN_SUCCESS)
                    }else{
                        liveError.postValue(msg)
                    }
                }

                onBizError { code, msg ->
                    liveError.postValue(msg)
                }
            }.onFailure {
                liveNetError.postValue(it.message)
            }
    }

    private fun saveLoginInfo(phone: String, token: String){
        UserInfoUtil.getInstance().updateLoginInfo(LoginInfo(phone, token))
    }

    private fun saveUserInfo(userInfo: UserInfo){
        UserInfoUtil.getInstance().updateUserInfo(userInfo)
    }

    /**
     * 微信授权code
     */
    fun wxLogin(code: String) = launch {
        repo.wxLogin(code)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    if (data != null){
                        saveLoginInfo("",data)
                    }
                    liveWXLogin.postValue(data)
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun getUserInfo() = launch {
        repo.getUserInfo()
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    data?.let {
                        saveUserInfo(it)
                        liveUserInfo.postValue(it)
                    }
                }
            }
    }

    fun getBindingInfo() = launch {
        repo.getBindingInfo()
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    liveBindingInfo.postValue(data)
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }

                onBizEmpty {
                    liveEmpty.postValue(null)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun bindWeChat(code: String) = launch {
        repo.bindWeChat(code)
            .serverData()
            .onSuccess {
                onBizOk { msg, data ->
                    liveBindWX.postValue(true)
                }

                onBizError { code, msg ->
                    liveError.postValue(msg)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun bindPhone(phone: String,code: String) = launch {
        repo.bindPhone(LoginRequest(phone, code))
            .serverData()
            .onSuccess {
                onBizOk { msg, data ->
                    liveBindPhone.postValue(true)
                }

                onBizError { code, msg ->
                    liveError.postValue(msg)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }
}